package cn.zucc.edu.madq.controller;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.util.JSONPObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zucc.edu.madq.service.MService;
import cn.zucc.edu.madq.service.MemorabiliaService;
import cn.zucc.edu.madq.pojo.ClubMemorabilia;
import cn.zucc.edu.madq.pojo.User;

@Controller
@RequestMapping("memorabilia")
public class MemorabiliaController {
	@Autowired
	private MemorabiliaService memorabiliaService;
	@Autowired
	private MService mService;
	private static Logger logger = Logger
			.getLogger(MemorabiliaController.class);

	@RequestMapping("getMByUserIdAndClubId")
	@ResponseBody
	public JSONPObject getMByUserIdAndClubId(
			@RequestParam("jsonpCallback") String jsonpCallback,
			@RequestParam("clubId") int clubId,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> m = mService.getMByUserIdAndClubId(user.getUserId(),clubId);
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, m);
		} catch (Exception e) {
			count = -1;
			throw e;
//			logger.info("------end------" + request.getPathInfo()
//					+ "------time------" + System.currentTimeMillis());
//			return new JSONPObject(jsonpCallback, count);
		}

	}
	
	@RequestMapping("getMNameByUserId")
	@ResponseBody
	public JSONPObject getMNameByUserId(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> m = mService.getMNameByUserId(user.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, m);
		} catch (Exception e) {
			count = -1;
			//throw e;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}
	
	@RequestMapping("insert")
	@ResponseBody
	public JSONPObject insert(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response,
			String title, String content, int clubId) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		ClubMemorabilia m = new ClubMemorabilia();
		m.setClubId(clubId);
		m.setClubMemorabiliaContent(content);
		m.setClubMemorabiliaTitle(title);
		m.setClubMemorabiliaDate(new Timestamp(System.currentTimeMillis()));
		try {
			if (null != user.getUserId() && user.getUserId() != 0) {
				count = memorabiliaService.insert(m);
			}
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}

	@RequestMapping("getMByUserId")
	@ResponseBody
	public JSONPObject getM(
			@RequestParam("jsonpCallback") String jsonpCallback,
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("------star------" + request.getPathInfo()
				+ "------time------" + System.currentTimeMillis());
		User user = (User) request.getSession().getAttribute("user");
		int count = 0;
		try {
			List<Map<String, Object>> m = mService.getMByUserId(user.getUserId());
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, m);
		} catch (Exception e) {
			count = -1;
			logger.info("------end------" + request.getPathInfo()
					+ "------time------" + System.currentTimeMillis());
			return new JSONPObject(jsonpCallback, count);
		}

	}

}
